package com.gxuwz.restaurant.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.*;


import com.gxuwz.restaurant.mapper.ApplygoodsMapper;
import com.gxuwz.restaurant.mapper.ApplyinfoMapper;
import com.gxuwz.restaurant.utils.ExcelUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/replenshment")
public class ReplenshmentController {

    @Resource
    ApplygoodsMapper applygoodsMapper;

    @Resource
    ApplyinfoMapper applyinfoMapper;

    String date;

    @PostMapping ("/apply")//post用来新增
    public Result<?> apply(@RequestBody Replenshment replenshment){

        Applyinfo applyinfo = new Applyinfo();
        applyinfo.setApplyname(replenshment.getApplyName());
        applyinfo.setDate(replenshment.getDate());
        applyinfo.setState("未处理");
        applyinfoMapper.insert(applyinfo);


        List<Applygoods> list = replenshment.getList();
  for (Applygoods list1: list){

    System.out.println(list1);
    Applygoods applygoods =new Applygoods();
    applygoods.setApplyname(replenshment.getApplyName());
    applygoods.setCount(list1.getCount());
    applygoods.setGoodsname(list1.getGoodsname());
    applygoods.setDate(replenshment.getDate());
    System.out.println(applygoods);
    applygoodsMapper.insert(applygoods);

}


        return Result.success();
    }

    @GetMapping("/check")   //get用来查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        LambdaQueryWrapper<Applyinfo> wrapper = Wrappers.<Applyinfo>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Applyinfo::getApplyname,search);
        }

        Page<Applyinfo> applyPage =  applyinfoMapper.findPage(new Page<>(pageNum,pageSize));



        return Result.success(applyPage );


    }

    @PutMapping("/finish/{id}")//put用来更新
    public Result<?> finish(@PathVariable int id) {

    Applyinfo applyinfo =new Applyinfo();
        applyinfo.setId(id);
        applyinfo.setState("已处理");
        applyinfoMapper.updateById(applyinfo);
        System.out.println(applyinfoMapper.updateById(applyinfo));
        return Result.success(applyinfoMapper.updateById(applyinfo));
    }

//    //导出补货表
@GetMapping("/getdate")   //传入日期给导出做查询
public Result<?> getdate(@RequestParam String date
                          ){
        this.date=date;

    System.out.println(this.date);
        return Result.success(this.date);
}
    @GetMapping("/export")
    public void export(HttpServletResponse response
                       ) throws IOException {
        QueryWrapper<Applygoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date", this.date);

        List<Map<String, Object>> list = CollUtil.newArrayList();
        List<Applygoods> all = applygoodsMapper.selectList(queryWrapper);
        for (Applygoods applygoods : all) {
            Map<String, Object> row1 = new LinkedHashMap<>();

            row1.put("商品",  applygoods.getGoodsname());
            row1.put("需要数量/重量(kg)", applygoods.getCount());

            list.add(row1);
        }

        String fileName = URLEncoder.encode("补货申请","UTF-8");
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);    }

//
//    /**
//     * Excel 导入
//     * 导入的模板可以使用 Excel 导出的文件
//     * @param file Excel
//     * @return
//     * @throws IOException
//     */
//    @PostMapping("/import")
//    public Result<?> upload(MultipartFile file) throws IOException {
//        InputStream inputStream = file.getInputStream();
//        List<List<Object>> lists = ExcelUtil.getReader(inputStream).read(1);
//        List<Applygoods> saveList = new ArrayList<>();
//        for (List<Object> row : lists) {
//            Applygoods applygoods = new Applygoods();
//            applygoods.setGoodsname(row.get(0).toString());
//            applygoods.getCount(row.get(1).toString());
//            applyinfo.setAge(Integer.valueOf(row.get(2).toString()));
//            applyinfo.setSex(row.get(3).toString());
//            applyinfo.setAddress(row.get(4).toString());
//            applyinfo.setRole(Integer.valueOf(row.get(5).toString()));
//            saveList.add(user);
//        }
//        for (User user : saveList) {
//            userMapper.insert(user);
//        }
//        return Result.success();
//    }

    }










